﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using XCI.Helper;
using XCI.Windows.Forms;
using XCI.Windows.Helper;
using XCI.Sys.Settings;
using XCI.Config;

namespace XCI.Sys.Forms
{
    public partial class FrmFullLogin : FrmBaseLogin
    {
        public FrmFullLogin()
        {
            InitializeComponent();
            var defaultFont = AppearanceObject.DefaultFont;
            this.label1.Font = new Font(defaultFont.Name, defaultFont.Size, FontStyle.Bold);
        }

        protected override System.Windows.Forms.Control EditAccount
        {
            get { return editAccount; }
        }

        protected override System.Windows.Forms.Control EditPassword
        {
            get { return editPassword; }
        }

        protected override System.Windows.Forms.Control BtnLogin
        {
            get { return btnLogin; }
        }

        protected override void LoadLoginSetting()
        {
            editAutoLogin.Checked = LoginFullSetting.Instance.AutoLogin;
            editSavePwd.Checked = LoginFullSetting.Instance.IsRememberPassword;
            editAccount.Text = LoginFullSetting.Instance.RememberLoginName;
            if (StringHelper.IsBase64(LoginFullSetting.Instance.RememberPassword))
            {
                editPassword.Text = EncryptHelper.SymmetricDecrypt(LoginFullSetting.Instance.RememberPassword,
                    EncryptHelper.SecretKey);
            }

            if (LoginFullSetting.Instance.AutoLogin)
            {
                ExecuteAutoLogin();
            }
        }

        protected override void SaveLoginSetting()
        {
            string loginName = editAccount.Text.Trim();
            string password = editPassword.Text;
            LoginFullSetting.Instance.AutoLogin = editAutoLogin.Checked;
            LoginFullSetting.Instance.IsRememberPassword = editSavePwd.Checked;

            if (editSavePwd.Checked)
            {
                LoginFullSetting.Instance.RememberLoginName = loginName;
                LoginFullSetting.Instance.RememberPassword =
                    EncryptHelper.SymmetricEncrypt(password, EncryptHelper.SecretKey);
            }
            else
            {
                LoginFullSetting.Instance.RememberLoginName = null;
                LoginFullSetting.Instance.RememberPassword = null;
            }

            ConfigFactory.Default.Save();
        }

        private void FrmLoginFull_Load(object sender, EventArgs e)
        {
            this.Text = $"{SysRuntime.Setting.Title} 系统登录";
            LoadLoginSetting();
            //NativeMethods.SetForegroundWindow(this.Handle);
        }

        private void editAccount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13 && editAccount.Text.Trim().Length > 0)
            {
                editPassword.Select();
                e.Handled = true;
            }
        }

        private void editPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnLogin.PerformClick();
                e.Handled = true;
            }
            else if (e.KeyChar == 27)
            {
                editAccount.Select();
                editAccount.SelectionStart = editAccount.Text.Length;
                e.Handled = true;
            }
        }

        private void editSavePwd_CheckedChanged(object sender, EventArgs e)
        {
            if (!editSavePwd.Checked)
            {
                editAutoLogin.Checked = false;
            }
        }

        private void editAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (editAutoLogin.Checked)
            {
                editSavePwd.Checked = true;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.LoginCore();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
            this.editAccount.Select();
        }
    }
}